<template>
    <div class="app-container customer-div">
<!--       <el-button-group>-->
<!--          <el-button @click="toDisGoods">排货列表</el-button>-->
<!--          <el-button type="primary">采购提货</el-button>-->
<!--        </el-button-group>-->
        <div style="margin-top: 40px">
            <!--搜索-->
            <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
                <el-form-item label="采购单号：" >
                    <el-input v-model="listQuery.orderSn"></el-input>
                </el-form-item>
                <el-form-item label="供应商名称：">
                    <el-select
                            @select="handleSelectSupplierName"
                            clearable
                            v-model="listQuery.supplierName"
                            filterable
                            remote
                            reserve-keyword
                            placeholder="请输入关键词"
                            :remote-method="remoteMethod3"
                            :loading="loading3">
                        <el-option
                                v-for="item in supplierList2"
                                :key="item.supplierName"
                                :label="item.supplierName"
                                :value="item.supplierName">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="业务员：">
                    <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable>
                        <el-option
                                v-for="item in userOptionList"
                                :key="item.userId"
                                :label="item.userName"
                                :value="item.userId">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="提货类型：" >
                    <el-select v-model="listQuery.planSendType" filterable placeholder="请选择" clearable>
                        <el-option
                                key="运联发货"
                                label="运联发货"
                                value="运联发货">
                        </el-option>
                        <el-option
                                key="工厂发货"
                                label="工厂发货"
                                value="工厂发货">
                        </el-option>
                    </el-select>
                </el-form-item>
<!--                <el-form-item label="送货类型：" >-->
<!--                    <el-select v-model="listQuery.isYunlianDeliver" filterable placeholder="请选择" clearable>-->
<!--                        <el-option-->
<!--                                :key="1"-->
<!--                                label="运联发货"-->
<!--                                :value="1">-->
<!--                        </el-option>-->
<!--                        <el-option-->
<!--                                :key="0"-->
<!--                                label="采购发货"-->
<!--                                :value="0">-->
<!--                        </el-option>-->
<!--                    </el-select>-->
<!--                </el-form-item>-->
                <el-form-item label="商品信息">
                    <el-input v-model="listQuery.goodsKeyword"></el-input>
                </el-form-item>
                <el-form-item label="提货日期：">
                    <el-date-picker
                            :default-time="['00:00:00', '23:59:59']"
                            v-model="listQuery.takeTime"
                            type="daterange"
                            format="yyyy 年 MM 月 dd 日"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="发货状态：" >
                    <el-select v-model="listQuery.planState" filterable placeholder="请选择" clearable>

                        <el-option
                                key="0"
                                label="未发货"
                                value="0">
                        </el-option>
                        <el-option
                                key="1"
                                label="部分发货"
                                value="1">
                        </el-option>
                        <el-option
                                key="2"
                                label="已发货"
                                value="2">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button
                            @click="handleSearchList()"
                            type="primary">
                        查询
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
        <!--数据列表-->
        <div class="table-container">
            <el-table
                    v-loading="listLoading"
                    :data="tableData"
                    border
                    style="width: 100%; margin-top: 20px">
                <el-table-column  label="采购单号" width="150" align="center" prop="orderSn">
                </el-table-column>
                <el-table-column  label="供应商名称" width="300">
                    <template slot-scope="scope">
                        <span>{{scope.row.supplierName}}</span>
                    </template>
                </el-table-column>
                <el-table-column  label="品名" width="150" align="center">
                    <template slot-scope="scope">
                        {{scope.row.goodsName}}
                    </template>
                </el-table-column>
                <el-table-column  label="商品牌号" width="150" prop="goodsSn">
                </el-table-column>
                <el-table-column  label="合同数量" width="100" align="center">
                    <template slot-scope="scope">
                        {{scope.row.goodsNum}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="提货仓库"
                        width="150">
                    <template slot-scope="scope">
                        {{scope.row.warehouseName}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="提货日期"
                        width="100"
                        align="center">
                    <template slot-scope="scope">
                        {{scope.row.takeTime}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="确定能否提货"
                        width="120"
                        align="center">
                    <template slot-scope="scope">
                        {{scope.row.isSure == 1 ? '确定' : '不确定'}}
                    </template>
                </el-table-column>
                <el-table-column
                        label="提货数量"
                        width="80"
                        align="center">
                    <template slot-scope="scope">
                        {{scope.row.takeNum}}
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        label="已发货数量"
                        width="100">
                    <template slot-scope="scope">
                        {{scope.row.actualShipNum}}
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        label="发货状态"
                        width="100">
                    <template slot-scope="scope">
                        {{scope.row.planState | planState}}
                    </template>
                </el-table-column>
                <el-table-column  label="交易方式" width="150" align="center">
                    <template slot-scope="scope">
                        {{scope.row.deliveryType}}
                    </template>
                </el-table-column>

                <el-table-column
                        align="center"
                        label="提货类型"
                        width="100">
                    <template slot-scope="scope">
                        {{scope.row.planSendType}}
                    </template>
                </el-table-column>


                <el-table-column
                        label="操作时间"
                        width="150"
                        align="center">
                    <template slot-scope="scope">
                        {{scope.row.updatedAt}}
                    </template>
                </el-table-column>
                <el-table-column
                        width="100"
                        fixed="right"
                        label="操作"
                        align="center">
                    <template slot-scope="scope">
                            <el-button
                                    style="margin-left:0"
                                    type="text"
                                    size="mini"
                                    @click="showDialog3(scope.row)">提货信息
                            </el-button>
                            <el-button
                                    v-if="scope.row.isNeedCheck == 1"
                                    style="margin-left:0"
                                    type="text"
                                    size="mini"
                                    @click="showDialogVisible(scope.row)">确认入库
                            </el-button>
                    </template>
                </el-table-column>

            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    layout="total, sizes,prev, pager, next,jumper"
                    :current-page.sync="listQuery.pageNum"
                    :page-size="listQuery.pageSize"
                    :page-sizes="[10,15,20]"
                    :total="count">
            </el-pagination>
        </div>

        <!--提货信息弹窗-->
        <el-dialog
                :close-on-click-modal="false"
                title="提货信息"
                :visible.sync="dialogVisible3"
                width="400px">
            <el-form size="mini" label-width="90px"  ref="priceForm" >
                <el-form-item label="商品信息：" style="margin-bottom:0px">
                        {{priceForm.goodsName}}
                </el-form-item>
                <el-form-item label="提货仓库：" style="margin-bottom:0px">
                    {{priceForm.warehouseName}}
                </el-form-item>
                <el-form-item label="提货时间：" style="margin-bottom:0px">
                    {{priceForm.takeTime}}
                </el-form-item>
                <el-form-item label="提货地址：" style="margin-bottom:0px">
                    {{priceForm.takeAddress}}
                </el-form-item>
                <el-form-item label="提货数量：" style="margin-bottom:0px">
                    {{priceForm.takeNum}}
                </el-form-item>
                <el-form-item label="联系人：" style="margin-bottom:0px">
                    {{priceForm.receiverName}}
                </el-form-item>
                <el-form-item label="手机号：" style="margin-bottom:0px">
                    {{priceForm.receiverMobile}}
                </el-form-item>
                <el-form-item label="注意事项：">
                    {{priceForm.takeRemark}}
                </el-form-item>
            </el-form>
        </el-dialog>


        <!--确认发货-->
        <el-dialog
                title="确认入库"
                :visible.sync="dialogVisible2"
                width="800px">
            <div style="max-height:500px;overflow: auto">
                <el-table ref="multipleTable"
                          row-key="dgId"
                          :data="sendList"
                          style="width: 95%;font-size: 12px;margin:0 auto"
                          @selection-change="handleSelectionChange"
                          v-loading="listLoading2"
                          border>
                    <el-table-column type="selection" :reserve-selection="true" width="60" align="center" :selectable="checkSelectable"></el-table-column>
                    <el-table-column label="入库单号" align="center" width="150">
                        <template slot-scope="scope">
                            {{scope.row.entrySn}}
                        </template>
                    </el-table-column>
                    <el-table-column label="采购单号" align="center" width="150">
                        <template slot-scope="scope">
                            {{scope.row.orderSn}}
                        </template>
                    </el-table-column>
                    <el-table-column label="商品信息" >
                        <template slot-scope="scope">
                            {{scope.row.goodsName}}
                        </template>
                    </el-table-column>
                    <el-table-column label="入库数量" align="center">
                        <template slot-scope="scope">
                            {{scope.row.shipNum}}
                        </template>
                    </el-table-column>
                    <el-table-column label="未核减数量" align="center">
                        <template slot-scope="scope">
                            {{scope.row.unPlanNum}}
                        </template>
                    </el-table-column>
                    <el-table-column label="送货类型" align="center">
                        <template slot-scope="scope">
                            {{scope.row.sendType}}
                        </template>
                    </el-table-column>
                </el-table>
                <div style="width: 95%;font-size: 12px;margin:20px auto 0 auto">
                    <div>需核减数量：<span style="font-size:14px;font-weight: bold">{{PlanNum}}</span> 吨 <span style="padding-left:20px">已选核减数量：<span style="color:#409EFF;font-weight: bold;font-size:14px">{{chooseUnPlanNum}}</span> 吨</span></div>
                    <div style="color:red;font-size:12px;margin-top:10px">说明：如果已选核减数量大于需核减数量，将按照选择的入库单顺序逐一核减，多出的入库数量将不会被核减</div>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                  <el-button @click="handleClose2">取 消</el-button>
                  <el-button type="primary" @click="handleConfirm2">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    import {getCustomerOption,getOrderUsersList,getEntryGoods,purchasePlanGoodsOperate} from '@/api/partner'
    import {getGoodsPlanList,getPurchasePlanGoodsList,supplierListApi} from '@/api/company'
    import {formatDate} from '@/utils/date';
    import store from '@/store'
    import { mapGetters } from 'vuex'
    const defaultListQuery={
        pageNum: 1,
        pageSize: 10,
        takeTime: [],
        orderSn:null,
        supplierName:null,
        userId:null,
        planSendType:'',
        goodsKeyword:'',
        planState:null,
    };
    const defaultPriceForm={
        goodsName: '',
        warehouseName: '',
        takeTime:'',
        takeAddress:'',
        takeNum:'',
        receiverName:'',
        receiverMobile:'',
        takeRemark:'',
    };
    export default {
        name: "index",
        data(){
            return {
                listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
                tableData: [],
                count: 0,
                listLoading: false,
                roles:store.getters.roles ? store.getters.roles : [],
                userOptionList:[],
                dialogVisible3:false,
                goodsPlanList:[],
                priceForm:JSON.parse(JSON.stringify(defaultPriceForm)),
                dialogVisible2:false,
                sendList:[],
                selectList:[],
                listLoading2:false,
                chooseUnPlanNum:0,//已选核减数量
                PlanNum:0,//需核减数量
                ppgId:'',
                supplierList2:[],
                loading3:false
            }
        },
        computed: {
            ...mapGetters([
                'name',
                'mobile',
                'userId'
            ]),

        },
        created() {
            this.getList();
            this.getUserOptionList();
        },
        filters: {
            formatCreateTime(time) {
                if(time){
                    let date = new Date(time);
                    return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
                }else{
                    return '--'
                }
            },
            planState(val){
                if(val == 0){
                    return '未发货'
                }else if(val == 1){
                    return '部分发货'
                }else if(val == 2){
                    return '已发货'
                }
            },
        },
        methods:{
          toDisGoods(){
            this.$router.push({name:'dischargingGoods'})
          },
            //获取供应商列表
            remoteMethod3(query) {
                if(query && query!= ''){
                    this.loading3 = true;
                    //获取供应产列表
                    supplierListApi({tab:'all',supplierName:query,pageNum:1,pageSize:1000}).then(response => {
                        this.supplierList2 = response.list;
                        this.loading3 = false;
                    }).catch(()=>{
                        this.loading3 = false;
                    });

                }
            },
            //判断是否能选择
            checkSelectable(row,index){
                let flag = true;
                for (let i = 0; i < this.sendList.length; i++) {
                    if( parseFloat(row.unPlanNum) == 0){
                        flag = false
                    }
                }
                return flag
            },

            //显示确认发货弹窗
            showDialogVisible(item){
                this.dialogVisible2 = true;
                this.selectList = [];
                this.chooseUnPlanNum = 0;
                this.PlanNum = item.unShipNum;
                this.ppgId = item.ppgId;
                this.getChooseList(item);
            },

            //关闭确认发货弹窗
            handleClose2(){
                this.$refs.multipleTable.clearSelection();
                this.dialogVisible2 = false;
            },

            /**
             *获取选择发货单列表
             **/
            getChooseList(item) {
                this.listLoading2 = true;
                getEntryGoods({pogId:item.pogId}).then(response => {
                    this.listLoading2 = false;
                    this.sendList = response;
                });
            },

            //切换选择发货单
            handleSelectionChange(val){
                this.selectList = JSON.parse(JSON.stringify(val));
                this.countUnPlanNum();
            },

            //计算已选中发货单的未核减数量
            countUnPlanNum(){
                this.chooseUnPlanNum = 0
                this.selectList.forEach((el)=>{
                    this.chooseUnPlanNum = this.chooseUnPlanNum + parseFloat(el.unPlanNum)
                });
                this.chooseUnPlanNum = this.chooseUnPlanNum.toFixed(4)
            },

            //提交确认的发货单
            handleConfirm2(){
                if(this.selectList.length > 0){
                    let data = {};
                    data.ppgId = this.ppgId;
                    data.pegIds = [];
                    this.selectList.forEach((el)=>{
                        data.pegIds.push(el.pegId)
                    });
                    this.handleClose2();
                    purchasePlanGoodsOperate(data).then(response=>{
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration:1000
                        });
                        this.getList();
                    });
                }else{
                    this.$message({
                        message: '请选择要核减的发货单',
                        type: 'info',
                        duration:1000
                    });
                }
            },

            //显示提货信息
            showDialog3(item){
                this.dialogVisible3 = true;
                this.priceForm = JSON.parse(JSON.stringify(defaultPriceForm));
                this.priceForm.goodsName = item.goodsName + '/' + item.goodsSn;
                this.priceForm.warehouseName = item.warehouseName;
                this.priceForm.takeTime = item.takeTime;
                this.priceForm.takeNum = item.takeNum;
                this.priceForm.takeAddress = item.takeAddress;
                this.priceForm.receiverName = item.receiverName;
                this.priceForm.receiverMobile = item.receiverMobile;
                this.priceForm.takeRemark = item.takeRemark;
            },

            //获取员工下拉选项
            getUserOptionList(){
                getOrderUsersList().then((res)=>{
                    this.userOptionList = res;
                })
            },

            //选择员工名称
            handleSelectUserName(key,item){
                this.listQuery[key] = item.userId;
            },


            //客户名称输入匹配
            querySearch(queryString, cb){
                getCustomerOption({keywords:queryString}).then((res)=>{
                    cb(res);
                })
            },


            //选择供应商
            handleSelectSupplierName(item){
                this.listQuery.supplierName = item
            },

            /**
             * 切换选择状态
             */
            handleSelect(key, keyPath){
                this.listQuery = JSON.parse(JSON.stringify(defaultListQuery)),
                    this.listQuery.tab = key;
                this.handleSearchList();
            },
            handleSizeChange(val){
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = val;
                this.getList();
            },
            handleCurrentChange(val){
                this.listQuery.pageNum = val;
                this.getList();
            },

            handleSearchList(){
                this.listQuery.pageNum = 1;
                this.getList();
            },

            //获取列表
            getList() {
                this.listLoading = true;
                getPurchasePlanGoodsList(this.listQuery).then(response => {
                    this.tableData = response.list;
                    this.count = response.count;
                    this.listLoading = false;
                }).catch(()=>{
                    this.listLoading = false;
                });
            },

        },
    }
</script>

<style scoped lang="scss">
    .upload-div{
        text-align: center;
        position:relative;
        #upload{
            opacity: 0;
            position: absolute;
            z-index: 1;
            width:154px;
            height:40px
        }
    }
</style>
